from myelin.Ns import MYELINS
from nodetypes.core.DocumentOrdering import OrderByKeys
from nodetypes.ntypes.Document.showBase.MML import Document, Transform
import operator


class Document(Document):
    
    containsKey = 'mContains'
    groupingKey = u'contains'
    GROUPING_SORT_KEY = operator.itemgetter(0)
    
    def createContentContainer(self, context, node, locale):
        model = context.model         
        root = context.defaults.document_root
        
        # grouping mappings
        gmap = {}
        ungrouped = []
        
        # order nodes into their groupings
        for n in self.filterContainedNodes(
            context,
            [],
            model,
            locale
        ):
            g = n.mGrouping
            if g:
                for g in g:
                    m = gmap.get(g.rdf_value)
                    if m:
                        m[1].append(n)
                    elif not g.mHidden:
                        gmap[g.rdf_value] = (g,[n])
            else:
                ungrouped.append(n)
        
        # extract for opt
        nxa = node.xml_append
        nxc = node.xml_create_element
                    
        if not gmap and not ungrouped:
            nxa(
                nxc(
                    u'a:grouping', MYELINS,
                    attributes = {
                        u'role'         : u'container',
                        u'key'          : self.groupingKey,
                    }
                )
            )
            return node        
        
        # Order groupings
        # get groupints
        if gmap:
            gmap = OrderByKeys(
                model,
                gmap.values()
                #self.GROUPING_SORT_KEY
            )
            
            action = context.action
            acls = context.acls.checkNodeAcls
            
            ccn = self.createContainerNodes
            
            map = context.uriMapper.apply
            
            for g,n in gmap:               
                if n:
                    OrderByKeys(g, n)
                           
                g_join = g.rdf_join
                id = g.rdf_value                
                if acls(g.rdf_getDomain(),action):
                    ccn(
                        context,
                        nxa(
                            nxc(
                                u'grouping', MYELINS,
                                attributes = {
                                    u'role'         : u'container',
                                    u'key'          : self.groupingKey,
                                    u'node-id'      : id,
                                    u'uri'         : map(g.rdf_domain),
                                    u'tag'          : g_join('mTag'),
                                    u'title'        : g_join('dcTitle'),
                                    u'description'  : g_join('dcDescription'), 
                                    u'count'        : unicode(len(n)),
                                    u'permitted-action' : u'1',
                                }
                            )
                        ),
                        n,
                        root,
                        locale
                    )
                else:
                    ccn(
                        context,
                        nxa(
                            nxc(
                                u'grouping', MYELINS,
                                attributes = {
                                    u'role'         : u'container',
                                    u'key'          : self.groupingKey,
                                    u'node-id'      : id,
                                    u'uri'         : map(g.rdf_domain),
                                    u'tag'          : g_join('mTag'),
                                    u'title'        : g_join('dcTitle'),
                                    u'description'  : g_join('dcDescription'), 
                                    u'count'        : unicode(len(n))
                                }
                            )
                        ),
                        n,
                        root,
                        locale
                    )
        
        if ungrouped:
            OrderByKeys(model, ungrouped)
            title = model.mDefault_Grouping_Title
            if title:
                self.createContainerNodes(
                    context,
                    nxa(
                        nxc(
                            u'a:grouping', MYELINS,
                            attributes = {
                                u'role'         : u'container',
                                u'key'          : u'ungrouped-nodes',
                                u'count'        : unicode(len(ungrouped)),
                                u'title'        : title[0].rdf_value,
                            }
                        )
                    ),
                    ungrouped,
                    root,
                    locale
                )
            else:
                self.createContainerNodes(
                    context,
                    nxa(
                        nxc(
                            u'a:grouping', MYELINS,
                            attributes = {
                                u'role'         : u'container',
                                u'key'          : u'ungrouped-nodes',
                                u'count'        : unicode(len(ungrouped)),
                            }
                        )
                    ),
                    ungrouped,
                    root,
                    locale
                )
            
        return node    


# being memory concious :)
#del operator

# Transform
class Transform(Transform):
    
    newDocument = Document